package com.nxzh.de.executor.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyUtil {

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getNowTime(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    // 比较俩个时间字符串 入参1 本地时间  入参2 redis时间
    public static String compareDateString(String time1,String time2){
        String result = "0";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dateTime1 = null;
        Date dateTime2 = null;
        try {
            dateTime1 = format.parse(time1);
            dateTime2 = format.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dateTime1.before(dateTime2)) {
            //第一个时间早于第二个时间
            result = "0";
        } else if (dateTime1.after(dateTime2)) {
            result = "1";
        } else {
            // 两个时间相同
            result = "1";
        }
        return result;
    }

    // 十分钟后的时间
    public static String getNowTenMinute(int minutes){
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();

        // 将日期设置为当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());

        // 添加10分钟到当前时间
        calendar.add(Calendar.MINUTE, minutes);

        // 转为date
        Date date = calendar.getTime();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return format.format(date);
    }

//    public static void main(String[] args) {
//        System.out.println("getNowTime() = " + getNowTime());
//        System.out.println("getNowTenMinute(600) = " + getNowTenMinute(600));
//        System.out.println("时间比较 = " + compareDateString("2024-03-20 23:00:00",getNowTenMinute(600)));
//
////        System.out.println("dateDifferent = " + dateDifferent("2024-03-20 23:00:00",getNowTenMinute(600)));
//
//        System.out.println("getSubMinutes(\"2024-03-20 23:00:00\") = " + getSubMinutes("2024-03-20 23:00:00"));
//        System.out.println("getSubMinutes(\"2024-03-20 12:00:00\") = " + getSubMinutes("2024-03-20 12:00:00"));
//
//    }


    // 返回相差的分钟数
    public static long getSubMinutes(String time){
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date c = sbf.parse(time);
            long l = d.getTime() - c.getTime();
            System.out.println("111111111111相差多少毫秒:"+l);
            long min = l / (1000 * 60);
            System.out.println("22222222222222相差多少分钟:"+min);
            return min;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return -1;
    }


    public static Integer dateDifferent(String dateStart, String dateStop) {
//        String dateStart = "2022-04-01 12:20:43";
//        String dateStop = "2022-04-08 15:57:48";
        long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = format.parse(dateStart);//毫秒ms
            Date d2 = format.parse(dateStop);//毫秒ms
            diff = d2.getTime() - d1.getTime();
            diffSeconds = diff / 1000 % 60;
            diffMinutes = diff / (60 * 1000) % 60;
            diffHours = diff / (60 * 60 * 1000) % 24;
            diffDays = diff / (24 * 60 * 60 * 1000);
            System.out.print("两个时间相差:");
            System.out.print(diffDays + "天,");
            System.out.print(diffHours + "小时,");
            System.out.print(diffMinutes + "分钟,");
            System.out.print(diffSeconds + "秒.");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int days = (int) diffDays;
        return days;
    }


}
